package com.medishare.fragment;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.medishare.R;
import com.medishare.adapter.AdapterMediRemiander;
import com.medishare.data.DataMediReminder;
import com.medishare.util.Utils;

import android.app.Activity;
import android.app.DialogFragment;
import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

public class FragmentMediReminder extends Fragment {

	private ListView mListView;
	private Activity mainActivity;
	private TextView mPromptTV;
	public final static String TAG = FragmentMediReminder.class.getSimpleName();
	public final static boolean DEBUG = true;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_medi_reminder, null);
		mListView = (ListView) view.findViewById(R.id.list_medi_reminder);
		mPromptTV = (TextView) view.findViewById(R.id.prompt);
		String url = Utils.getBaseUrl() + "Drug/read/?uuid="
				+ Utils.getDeviceUUID(mainActivity);
		if (DEBUG) {
			Log.i(TAG, url);
		}
		loadData(url);
		return view;
	}

	private void loadData(String url) {
		AsyncHttpClient client = new AsyncHttpClient();
		final ArrayList<DataMediReminder> datas = new ArrayList<DataMediReminder>();
		client.get(url, new AsyncHttpResponseHandler() {
			Message msg = new Message();

			@Override
			public void onSuccess(String content) {
				super.onSuccess(content);

				try {

					JSONObject jsonObject = new JSONObject(content);
					int status = jsonObject.getInt("status");
					if (status == 0) {
						msg.what = 1;
						mHandler.sendMessage(msg);
						return;
					}
					JSONArray jsonArray = jsonObject.getJSONArray("data");
					for (int i = 0; i < jsonArray.length(); i++) {
						JSONObject tempObject = (JSONObject) jsonArray.get(i);
						DataMediReminder data = new DataMediReminder();
						data.setId(tempObject.getString("id"));
						data.setMember_id(tempObject.getString("member_id"));
						data.setMedicine_id(tempObject.getString("medicine_id"));
						data.setMdc_name(tempObject.getString("mdc_name"));
						data.setMdc_rx(tempObject.getString("mdc_rx"));
						data.setMdc_usage(tempObject.getString("mdc_usage"));
						data.setRemark(tempObject.getString("remark"));
						data.setStatus(tempObject.getString("status"));
						data.setCreate_time(tempObject.getString("create_time"));
						data.setUpdate_time(tempObject.getString("update_time"));
						data.setName(tempObject.getString("name"));
						datas.add(data);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}

				msg.what = 0;
				msg.obj = datas;
				mHandler.sendMessage(msg);
			}

			@Override
			public void onFailure(Throwable error, String content) {
				msg.what = -1;
				mHandler.sendMessage(msg);
				super.onFailure(error, content);
			}

		});
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
			dialog = ProgressDialogFragment.newInstance();
			dialog.show(getFragmentManager().beginTransaction(), "progress");
		
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mainActivity = getActivity();
	}

	Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (dialog != null && dialog.getDialog().isShowing()) {
				dialog.dismiss();
			}
			switch (msg.what) {
			case 0:
				ArrayList<DataMediReminder> datas = (ArrayList<DataMediReminder>) msg.obj;
				AdapterMediRemiander adapter = new AdapterMediRemiander(
						mainActivity, datas);
				mListView.setAdapter(adapter);
					mListView.setVisibility(View.VISIBLE);
					mPromptTV.setVisibility(View.GONE);
				break;
			case 1:
				mListView.setVisibility(View.GONE);
				mPromptTV.setVisibility(View.VISIBLE);
				break;
			case -1:
				mListView.setVisibility(View.GONE);
				mPromptTV.setVisibility(View.VISIBLE);
				mPromptTV.setText(mainActivity.getResources().getString(
						R.string.server_error));
			}
		}

	};
	private DialogFragment dialog;

}
